package com.brayan.webapp.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;

import com.brayan.webapp.model.Pub;
import com.brayan.webapp.model.PubCategory;
import com.brayan.webapp.repository.PubCategoryRepository;
import com.brayan.webapp.repository.PubRepository;

@Stateless
public class PubBean {

	@Inject
	PubRepository pubRepository;
	@Inject
	PubCategoryRepository pubCategoryRepository;

	public Pub getPubById(Long pubId) {
		return pubRepository.getPubById(pubId);
	}

	public List<Pub> getPubsByCategory(Long pubCategoryId) {
		List<Pub> pubList = new ArrayList<Pub>();
		return pubList;
	}

	public List<PubCategory> getAllActivePubCategories() {
		return pubCategoryRepository.getAllActivePubCategories();
	}

	/**
	 * TODO, check if priviledged! Save the publication in the repository.
	 * 
	 * @param pub
	 */
	public void updatePub(Pub pub) {
		// TODO check if allowed to updated.
		if (pub != null) {
			// get the private pub category and set as default.
			if (pub.getPubCategory() == null) {
				PubCategory defaultPubCat = pubCategoryRepository
						.getDefaultPubCategory();
				pub.setPubCategory(defaultPubCat);
			}
			pubRepository.updatePub(pub);
		}
	}

}
